import { nav, back, makePhoneCall, rep, swi, copyText, matchSearchKey } from "@/utils"
import permision from "@/js_sdk/wa-permission/permission.js"
import dayjs from "dayjs"
import coordtransform from "coordtransform"
export default {
	methods: {
		nav,
		back,
		rep,
		swi,
		copyText,
		makePhoneCall,
		matchSearchKey,
		toJSON(){},
		// 税费
		setTaxation(price, tax){
			if(!price) return ""
			let money = Number(price) * Number(tax)
			return money.toFixed(2)
		},
		/**格式化时间
		 * @param {Object} value
		 * @param {Object} type
		 */
		formatParamTime(value, type){
			let formatType = "YYYY-MM-DD"
			if(type === "year-month"){
				formatType = "YYYY-MM"
			}else if(type === 'datetime'){
				formatType = "YYYY-MM-DD HH:mm:ss"
			}
			if(typeof value === "string"){
				return dayjs(value).valueOf()
			}else if(typeof value === "number"){
				return dayjs(value).format(formatType)
			}
			return value
		},
		formatTimeConf(data, conf){
			if(!data) return;
			try{
				let obj = JSON.parse(JSON.stringify(data))
				Object.entries(conf).reduce((acc, [key, value]) => {
					obj[key] = this.formatParamTime(obj[key], value)
				}, {})
				console.log(obj, 'err')
				return obj
			}catch(e){
				return data
			}
		},
		// vue的method里编写如下代码
		async requestAndroidPermission(permisionID) {
		    var result = await permision.requestAndroidPermission(permisionID)
		    var strStatus
		    if (result == 1) {
		        strStatus = "已获得授权"
		    } else if (result == 0) {
		        strStatus = "未获得授权"
		    } else {
		        strStatus = "被永久拒绝权限"
		    }
		    uni.showModal({
		        content: permisionID + strStatus,
		        showCancel: false
		    });
		},
		openMap(opt){
			// #ifdef H5
				console.warn("H5暂不支持打开外部应用")
			// #endif
			// #ifdef APP-PLUS
				// 百度坐标
				let bd09 = coordtransform.gcj02tobd09(opt.lng, opt.lat)
						console.log(`${bd09[1]},${bd09[0]}`)
				var url = "";
				if (plus.os.name=="Android") {
					var hasBaiduMap = plus.runtime.isApplicationExist({pname:'com.baidu.BaiduMap',action:'baidumap://'});
					var hasAmap = plus.runtime.isApplicationExist({pname:'com.autonavi.minimap',action:'androidamap://'});
					var urlBaiduMap = `baidumap://map/marker?location=${bd09[1]},${bd09[0]}&title=${opt.title}&src=南天快修工程师`;
					var urlAmap = `androidamap://viewMap?sourceApplication=南天快修工程师&poiname=${opt.title}&lat=${opt.lat}&lon=${opt.lng}&dev=0`
					if (hasAmap && hasBaiduMap) {
						plus.nativeUI.actionSheet({title:"选择地图应用",cancel:"取消",buttons:[{title:"百度地图"},{title:"高德地图"}]}, function(e){
							switch (e.index){
								case 1:
									plus.runtime.openURL(urlBaiduMap);
									break;
								case 2:
									plus.runtime.openURL(urlAmap);
									break;
							}
						})
					}
					else if (hasAmap) {
						plus.runtime.openURL(urlAmap);
					}
					else if (hasBaiduMap) {
						plus.runtime.openURL(urlBaiduMap);
					}
					else{
						url = `geo:${bd09[1]},${bd09[0]}?q=${opt.title}`;
						plus.runtime.openURL(url); //如果是国外应用，应该优先使用这个，会启动google地图。这个接口不能统一坐标系，进入百度地图时会有偏差
					}
				} else{
					console.log(plus.os.name)
					// iOS上获取本机是否安装了百度高德地图，需要在manifest里配置，在manifest.json文件app-plus->distribute->apple->urlschemewhitelist节点下添加（如urlschemewhitelist:["iosamap","baidumap"]）
					plus.nativeUI.actionSheet({
						title:"选择地图应用",
						cancel:"取消",
						buttons:[
							{title:"Apple地图"},
							{title:"百度地图"},
							{title:"高德地图"},
						],
					}, function(e){
						console.log("e.index : " ,e);
						let title = encodeURIComponent(opt.title)
						let sourceApplication = encodeURIComponent('南天快修工程师')
						switch (e.index){
							case 1:
								url = `http://maps.apple.com/?q=${title}&ll=${opt.lat},${opt.lng}`;
								break;
							case 2:
								url = `baidumap://map/marker?location=${bd09[1]},${bd09[0]}&title=${title}&content=${title}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
								break;
							case 3:
								url = `iosamap://viewMap?sourceApplication=${sourceApplication}&poiname=${title}&lat=${opt.lat}&lon=${opt.lng}&dev=0`;
								break;
							default:
								break;
						}
						if (url!="") {
							plus.runtime.openURL( url, function( e ) {
								console.log(e,'本机未安装指定的地图应用')
								plus.nativeUI.alert("本机未安装指定的地图应用");
							});
						}
					})
				}
			// #endif
		},
	}
}